package com.victor.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
	public static boolean after(String datestr1, String datestr2) throws ParseException {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(StrToDate(datestr1));
		Calendar c2 = Calendar.getInstance();
		c2.setTime(StrToDate(datestr2));
		return c2.after(c1);
	}

	public static Date StrToDate(String datestr) throws ParseException {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		date = formatter.parse(datestr);
		return date;
	}

	// 获取当日8点的时间
	public static Date gettime8() throws ParseException {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 8);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return timelong2date(c.getTimeInMillis());
	}

	// 获取当日18点时间
	public static Date gettime18() throws ParseException {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 18);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return timelong2date(c.getTimeInMillis());
	}

	// //时间戳转化为Sting或Date
	public static Date timelong2date(long time) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String d = format.format(time);
		Date date = format.parse(d);
		return date;
	}

	// 获取当前日期
	public static String getthisdate() {
		Date d = new Date();
		System.out.println(d);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(d);
		return dateNowStr;
	}

	// 得到前一天日期
	public static String getyesterdaydate() throws ParseException {
		Calendar c = Calendar.getInstance();// 此时打印它获取的是系统当前时间
		c.add(Calendar.DATE, -1); // 得到前一天
		return date2str(timelong2date(c.getTimeInMillis()));
	}

	public static String date2str(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(date);
		return dateString;
	}

	// 传入Date返回时分秒字符串
	public static String gettime_hms() {
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String dateString = formatter.format(date);
		return dateString;
	}

	// 获取当天24点时间
	public static Date gettoday24time() throws ParseException {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String d = format.format(date);
		return StrToDate(d + " 23:59:59");
	}

	/**
	 * 获取当前日期两年前日期(2020-11-01)
	 * 
	 * @return
	 */
	public static String twoyearafter() {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, 2);// 把日期往后增加一年.整数往后推,负数往前移动
		date = calendar.getTime();
		return date2str(date);
	}

	/**
	 * 获取传入时间2年前时间
	 * 
	 * @return
	 */
	public static String twoyearafter(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, 2);// 把日期往后增加一年.整数往后推,负数往前移动
		date = calendar.getTime();
		return date2str(date);
	}

	/**
	 * 传入时间截取到日
	 * 
	 * @param args
	 * @throws ParseException
	 */
	public static String jiequtori(String datestr) throws ParseException {
		return date2str(StrToDate(datestr));
	}

	/**
	 * 
	 */
	// 获取当前日期
	public static String getNow() {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String dateNowStr = sdf.format(d);
		return dateNowStr;
	}
	/**
	 * 获取半小时后时间
	 * 
	 * @param args
	 * @throws ParseException
	 */
	public static String getAfterBXS() {
		long currentTime = System.currentTimeMillis() + 30 * 60 * 1000;
		Date date = new Date(currentTime);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String BXSTime = "";
		BXSTime = df.format(date);
		return BXSTime;
	}

	public static void main(String[] args) throws ParseException {
		 System.out.println(getAfterBXS());
		// System.out.println(gettime18());

//		System.out.println(twoyearafter(StrToDate("2006-12-21 12:22:22")));
		// System.out.println(getyesterdaydate());
		// System.out.println(gettoday24time());
	}

}
